//
//  SearchViewController.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 1/5/17.
//  Copyright © 2017 com.mask. All rights reserved.
//

import UIKit
import MJRefresh

class SearchViewController: UIViewController {
    
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    
    var projects = [ProjectInfo]()
    var searchResult: [ProjectInfo]?
    var max_page: Int?
    let cur_page = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
    }
    
    func configureTableView() {
        // load more footer
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
    }
    
    func loadMoreData() {
        if cur_page < max_page! {
            HTTPRequest.projectListWith(keyword: "", page: 1) {response in
                
            }
        } else {
            tableView.mj_footer.endRefreshing()
        }
    }
}

extension SearchViewController: UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResult?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "")
        return cell!
    }
}

extension SearchViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let project = projects[indexPath.row]
        let vc = ProjectDetailController(id: project.id!)
        navigationController?.pushViewController(vc, animated: true)
    }
}
